package com.xuetang9.t9.hotel.controller;

import com.xuetang9.t9.hotel.beans.DiningTable;
import com.xuetang9.t9.hotel.beans.TableType;
import com.xuetang9.t9.hotel.services.IDiningTableService;
import com.xuetang9.t9.hotel.services.ITableTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: MyHotelProject
 * @Package: com.xuetang9.t9.hotel.controller
 * @ClassName: DiningTableController
 * @Author: ChinaHp
 * @Description: 餐桌控制层
 * @Date: 2020/2/24 17:02
 * @Version: 1.0
 */
@Controller
@RequestMapping("diningTable")
@ResponseBody
public class DiningTableController {
    @Autowired
    private IDiningTableService service;
    @Autowired
    private ITableTypeService tables;




    @PostMapping("zwlx")
    public List<TableType> getTableType(){
        return tables.tables();
    }

    @PostMapping("czlb")
    public List<DiningTable> query(){
        System.out.println(service.queryAll());
        return service.queryAll();
    }

    /**
     * 勿动
     * @return
     */
    @RequestMapping("/show")
    public List<DiningTable> showDin(){
        return service.selectByAllByType();
    }
    /**
     * 根据餐桌类型查询餐桌
     * @param id
     * @return
     */
    @RequestMapping("/table/{id}")
    public List<DiningTable> queryByTableId(@PathVariable("id") Integer id){
        return service.queryById(id);
    }

    @PostMapping("/tables")
    private List show(){
        return tables.tablesByArranging();
    }


    /**
     * 换桌操作
     * @param source
     * @param destination
     * @return
     */
    @RequestMapping("/change/{sourceId}/{destinationId}")
    private Map changeDiningTable(@PathVariable("sourceId") Integer source, @PathVariable("destinationId") Integer destination){
        Map map  = new HashMap();
        if(source == null || destination == null){
            map.put(400,"参数错误");
            return map;
        }
        if(!service.changeDiningTable(source,destination)){
            map.put(500,"操作错误");
            return map;
        }
        map.put(200,"操作成功");
        return map;
    }

    /**
     * 清桌操作
     * @param Tableid
     * @return
     */
    @RequestMapping("/cleantable/{id}")
    private Map cleanTable(@PathVariable("id") Integer Tableid){
        Map map  = new HashMap();
        if(Tableid == null){
            map.put(400,"参数错误");
            return map;
        }
        if(service.cleanTable(Tableid)){
            map.put(200,"操作成功");
            return map;
        }
        map.put(500,"操作错误");
        return map;
    }

    @PostMapping("delTable")
    public Boolean delTable(@RequestParam("id") String id){
        System.out.println("删除Id:" + id);
        if(service.del(Integer.parseInt(id))){
            System.out.println("删除成功");
            return true;
        }
        return false;
    }
    @PostMapping("addTable")
    public boolean addTable(@RequestParam("type")String type,@RequestParam("addNum")String addNum){
        System.out.println("类型:" +type);
        int num = Integer.parseInt(addNum);
        System.out.println("添加数量:" +num);
        TableType tableType = tables.queryByName(type);
        int typeId = tableType.getId();
        System.out.println("类型ID" + typeId);
        long maxId = service.maxId();
        System.out.println("最大ID:" + maxId);
        for (int i = 1; i <= num; i++) {
            DiningTable table = new DiningTable();
            table.setName(type + (maxId + i + 1));
            table.setTypeId(typeId);
            System.out.println("添加名称：" + table.getName());
            boolean bo = service.add(table);
            System.out.println("添加成功");
            if(i == num){
                return true;
            }
        }
        return false;
    }

    @PostMapping("addCZType")
    public boolean addCzType(@RequestParam("typeName")String name,@RequestParam("max")String max,@RequestParam("waitTime")String waitTime){
        System.out.println("添加类型名称：" + name);
        System.out.println("最大人数：" + max);
        TableType type = new TableType();
        type.setName(name);
        type.setDiners(Integer.parseInt(max));
        type.setWaitTime(Integer.parseInt(waitTime));
        if(tables.add(type)){
            System.out.println("添加成功");
            return true;
        }
        return false;
    }

    @PostMapping("delType")
    public boolean delType(@RequestParam("id")String id){
        service.delByTypeId(Integer.parseInt(id));
        System.out.println("删除类型Id:" + id);
        if(tables.del(Integer.parseInt(id))){
            System.out.println("删除成功");
            return true;
        }
        return false;
    }

    @PostMapping("alterTable")
    public boolean alterTable(@RequestParam("id")String id,@RequestParam("name")String name,@RequestParam("type")String type){
        System.out.println(id+name+type);
        DiningTable table = new DiningTable();
        table.setTableNum(Integer.parseInt(id));
        table.setName(name);
        TableType typeId = tables.queryByName(type);
        table.setTypeId(typeId.getId());
        if(service.update(table)){
            System.out.println("编辑成功");
            return true;
        }
        return false;
    }

    @PostMapping("alterType")
    public boolean alterType(@RequestParam("id")String id,@RequestParam("name")String name,@RequestParam("max")String max,@RequestParam("waitTime")String waitTime){
        TableType type = new TableType();
        type.setId(Integer.parseInt(id));
        type.setName(name);
        type.setDiners(Integer.parseInt(max));
        type.setWaitTime(Integer.parseInt(waitTime));
        if(tables.update(type)){
            System.out.println("编辑成功");
            return true;
        }
        return false;
    }

    @PostMapping("queryByType")
    public List<DiningTable> queryByType(@RequestParam("type")String type){
        System.out.println(type);
        TableType tableType = tables.queryByName(type);
        return service.queryById(tableType.getId());
    }
}
